import Vue from 'vue'
import Router from 'vue-router'
import Login from "../components/Login";
import Home from "../components/Home";
import Welcome from "../components/Welcome";
import Users from "../components/user/Users";
import Roles from "../components/power/Roles";
import Menus from "../components/power/Menus";
import Chat from "../components/chat/Chat";
import StudentCourse from "../components/course/StudentCourse";
import Coach from "../components/course/Coach";
import CoachCourse from "../components/course/CoachCourse";
import ExamineManage from "../components/examine/ExamineManage";
import AppointExamine from "../components/examine/AppointExamine";
import StudentExamine from "../components/examine/StudentExamine";
import CoachExamine from "../components/examine/CoachExamine";
import Apply from "../components/drivingSchool/Apply";
import ApplyManage from "../components/drivingSchool/ApplyManage";
import CoachMessage from "../components/personalInformation/CoachMessage";
import StudentMessage from "../components/personalInformation/StudentMessage";
import MessageStudent from "../components/personalInformation/MessageStudent";
import MessageCoach from "../components/personalInformation/MessageCoach";
import ApplyList from "../components/drivingSchool/ApplyList";
import LogManage from "../components/log/LogManage";

Vue.use(Router)

const router = new Router({
  routes: [
    {path: '/', redirect: "/login"},
    {path: '/login', component: Login},
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        {path: '/welcome', component: Welcome},
        {path: '/users', component: Users},
        {path: '/roles', component: Roles},
        {path: '/menus', component: Menus},
        {path: '/chat', component: Chat},
        {path: '/student/course', component: StudentCourse},
        {path: '/coach', component: Coach},
        {path: '/coach/course', component: CoachCourse},
        {path: '/examine/manage', component: ExamineManage},
        {path: '/appoint/examine', component: AppointExamine},
        {path: '/student/examine', component: StudentExamine},
        {path: '/coach/examine', component: CoachExamine},
        {path: '/apply', component: Apply},
        {path: '/apply/manage', component: ApplyManage},
        {path: '/coach/message', component: CoachMessage},
        {path: '/apply/list', component: ApplyList},
        {path: '/student/message', component: StudentMessage},
        {path: '/message/student', component: MessageStudent},
        {path: '/message/coach', component: MessageCoach},
        {path: '/log/manage', component: LogManage}
      ]
    }
  ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问路径
  // from 从哪个路径跳转过来
  // next 函数，表示放行 next()；放行，next('/login')：强制跳转

  if (to.path === '/login') return next()
  //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()

})

export default router
